/**
 * <p>Title:时间工具包</p>
 * <p>Description:时间工具包</p>
 * <p>Copyright:Copyright (c) 2010</p>
 * <p>Company:易泓咨询管理公司 </p>
 * <p>Date:2010-12-27</p>
 * <p>Modify:</p>
 * <p>Bug:</p>
 * @author 
 * @version 1.0
 */
package com.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

	private static final String DATE_START = " 00:00:00";
	private static final String DATE_END = " 23:59:59";

	/**
	 * 将date提早至当天00:00:00
	 * 
	 * @param date参数日期
	 * @return返回延长后的日期
	 */
	public static Date formatDateStart(Date date) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String strd = sdf.format(date) + DATE_START;
			sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
			try {
				date = sdf.parse(strd);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return date;
		}
		return null;
	}

	/**
	 * 将date延长至当天23:59:59
	 * 
	 * @param date参数日期
	 * @return返回延长后的日期
	 */
	public static Date formatDateEnd(Date date) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String strd = sdf.format(date) + DATE_END;
			sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
			try {
				date = sdf.parse(strd);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return date;
		}
		return null;
	}

	public static String DateUpdate(String date, boolean s) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String newString = "";
		try {
			if (s) {
				Date dd = df.parse(date);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(dd);
				calendar.add(Calendar.DAY_OF_MONTH, 1);// 加一天
				newString = df.format(calendar.getTime());
				// System.out.println(date+"增加一天之后：" +
				// df.format(calendar.getTime()));
			} else {
				long dif = df.parse(date).getTime() - 86400 * 1000;// 减一天
				Date date1 = new Date();
				date1.setTime(dif);
				newString = df.format(date1);
				// System.out.println("减少一天之后：" + df.format(date1));
			}

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newString;
	}

	public static String checkDay(String date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String newString = "";
		Date beforeDay;
		Date dNow = new Date(); // 当前时间
		try {
			Date dd = df.parse(date);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(dNow);
			calendar.add(Calendar.DAY_OF_MONTH, -3);// 前三天
			beforeDay = calendar.getTime();
			if (dd.compareTo(beforeDay) == 0 || dd.compareTo(beforeDay) <= 0) {
				// 两个日期相等或小于
				newString = "1";
			} else
				newString = "0";

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newString;
	}
	/**
	 * 
	 * @param date 日期操作
	 * @param type 要增减的年(Y),月(M),日(D)
	 * @param day  加减天数(负为减，正为加)
	 * @return
	 */
	public String dateOperation(String date,String type,Integer day ) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date dd =null;
		try {
			if(date.length()==4){
				dd = df.parse(date+"-01-01");				
			}else if(date.length()==7){
				dd = df.parse(date+"-01");
			}else if(date.length()==10){
				dd = df.parse(date);
			}
			Calendar cal=Calendar.getInstance();
			cal.setTime(dd);
			
			System.out.println("未修改"+df.format(dd));
		    if("Y".equals(type)){//nian
				cal.add(Calendar.YEAR, day);
			}else if("M".equals(type)){//yue
				cal.add(Calendar.MONTH, day);
			}else if("D".equals(type)){//tian
				cal.add(Calendar.DATE, day);
			}
			dd=cal.getTime();
			System.out.println("修改后"+df.format(dd));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return df.format(dd);
	}

}